<?php
// This controller handles the logic for the setup page

class SetupController extends Controller
{
		/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	
	public function actionBuyer() 
	{
		if(Yii::app()->user->isGuest)
		{
			echo "please login first. We will redirect you to the login page in 3 secs";
			header("refresh:3;url=".Yii::app()->request->baseUrl);
		}
		else if(Yii::app()->user->userType=='s')
		{
			echo "you have no authority to view the buyer page";
		}
		else if(Yii::app()->user->roleType!=Yii::app()->params['userRoles'][1])
		{
			//TODO: should redirect to the payment page
			$this->redirect(Yii::app()->request->baseUrl.'/landing/buyer');
		}
		else
		{
			$site_model=new Sites;
			$site_model->scenario='create';
			$tank_model=new Tanks;
			$tank_model->scenario='create';
			$this->parseData($site_model, $tank_model);
			$this->render('buyer', array('site_model'=>$site_model, 'tank_model'=>$tank_model));
		}	
	}
	
	public function parseData($site_model, $tank_model)
	{
		if(isset($_POST['Sites']))
		{
			$site_model->site_name=$_POST['Sites']['site_name'];
			$site_model->ust_facility_ID=$_POST['Sites']['ust_facility_ID'];
			$site_model->organization_ID=Users::model()->findByPk(Yii::app()->user->userID)->organization_ID;
			$site_model->address=$_POST['Sites']['address'];
			$site_model->city=$_POST['Sites']['city'];
			$site_model->state=$_POST['Sites']['state'];
			$site_model->zip=$_POST['Sites']['zip'];
			$site_model->county=$_POST['Sites']['county'];
			$site_model->geocode=$_POST['Sites']['geocode'];
			$transaction = Yii::app()->db->beginTransaction();
			if($site_model->save())
			{
				if(isset($_POST['facilities']))
				{
					for($i=0;$i<count($_POST['facilities']);$i++)
					{
						$sitefaci_model=new SiteFaciRelation;
						$sitefaci_model->site_ID=$site_model->site_ID;
						$sitefaci_model->facility_ID=$_POST['facilities'][$i];
						if(!$sitefaci_model->save())
						{
							$transaction->rollback();
							return;
						}	
					}
				}
				$tank_model->tank_name=$_POST['Tanks']['tank_name'];
				$tank_model->site_ID=$site_model->site_ID;
				$tank_model->capacity=$_POST['Tanks']['capacity'];
				$tank_model->alert_capacity=CLocale::getInstance('en_US')->getNumberFormatter()->format("#########.######",$tank_model->capacity/2);
				$tank_model->warning_capacity=CLocale::getInstance('en_US')->getNumberFormatter()->format("#########.######",$tank_model->capacity/4);
				$tank_model->tank_position_ID=$_POST['Tanks']['tank_position_ID'];
				$tank_model->tank_type_ID=$_POST['Tanks']['tank_type_ID'];
				$tank_model->tank_monitor=$_POST['Tanks']['tank_monitor'];
				$tank_model->pump_provided=$_POST['Tanks']['pump_provided'];
				$tank_model->tank_drop_method_ID=$_POST['Tanks']['tank_drop_method_ID'];
				$tank_model->product_ID=$_POST['Tanks']['product_ID'];
				$tank_model->inventory_last_read=$tank_model->capacity;
				$tank_model->inventory_last_time=date('yy-m-d H:i:s');
				if($tank_model->save())
				{
					$transaction->commit();
					echo 'successful';
				}
				else
				{
					$transaction->rollback();
				}
			}
			else
			{
				$transaction->rollback();
			}
		}
	}
}
?>
